//
//  FLMDefaultViewController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/20.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMDefaultViewController.h"
#import "FLMBackProfitCell.h"

@interface FLMDefaultViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    
    FLMBackProfitCell * cell;
}
@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UITableView                * tabV;//表格

@end

@implementation FLMDefaultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatTabV];
    
    // 根据传来的index进行不同的数据请求
    [self senderRequestIndex];
}

#pragma mark --------------------
#pragma mark  根据传来的index进行不同的数据请求
- (void)senderRequestIndex
{
    
    switch (self.index) {
        case 0:
            
            //默认
            
            break;
            
        case 1:
            
            //返利率
            
            break;
            
        case 2:
            
            //限时抢
            
            break;
            
            
        case 3:
            
            //获利
            
            break;

        case 4:
            
            //热门
            break;
        default:
            break;
    }

}

#pragma mark-创建表格
-(void)creatTabV{
    
    _tabV = [[UITableView alloc]initWithFrame:CGRectMake(0, GETSIZE(60), ScreenW, ScreenH-GETSIZE(260)) style: UITableViewStylePlain];
    _tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabV.showsVerticalScrollIndicator = NO;
    _tabV.delegate = self;
    _tabV.dataSource = self;
    [self.view addSubview:_tabV];
    
//    //返回顶端按钮
//    UIView * footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
//    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(self.view.width-GETSIZE(70), GETSIZE(10), GETSIZE(50), GETSIZE(50));
//    [btn setImage:[UIImage imageNamed:@"回到顶端"] forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(backToFirst) forControlEvents:UIControlEventTouchUpInside];
//    [footV addSubview:btn];
//
//    _tabV.tableFooterView = footV;
}

#pragma mark --------------------
#pragma mark 滚动触发代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     if (scrollView.contentOffset.y < 128 && self.btn) {
         [self.btn removeFromSuperview];
         self.btn = nil;
     } else {
         if (self.btn == nil && scrollView.contentOffset.y > 128) {
             [self addButtonToTop];
         }
     }
 }

#pragma mark --------------------
#pragma mark 返回顶部按钮
- (void)addButtonToTop {
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 65, ScreenH - 64 - 60,60, 60)];
    //    _btn.backgroundColor = [UIColor orangeColor];
    [_btn setImage:[UIImage imageNamed:@"回到顶端"] forState:UIControlStateNormal];
    //    _btn.layer.cornerRadius = 8;
    [_btn setDragEnable:YES];
    [_btn setAdsorbEnable:YES];
    [[[UIApplication sharedApplication].windows lastObject] addSubview:_btn];
    //    [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
    //        [self.tabV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    //    }];
    
    [_btn addTarget:self action:@selector(backToFirst) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --------------------
#pragma mark 返回顶部
-(void)backToFirst{
    
    [self.tabV setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark-表格代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellId = @"cellId";
    cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        
        cell = [[FLMBackProfitCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
    }
    
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return GETSIZE(260);
}

@end
